<?php

namespace Forms;

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Submit;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Identical;
use Phalcon\Validation\Validator\Confirmation;
use Models\Roles;
use Models\References;
use Phalcon\Mvc\Model\Message as ModelMessage;
use Phalcon\Validation\Message;
use Phalcon\Validation\Message\Group;

class UserForm extends Form {

    public function initialize($entity = null, $options = null) {
        //Họ tên
        $name = new Text('fullName');
        $name->setLabel('Name');
        $name->addValidators(array(
            new PresenceOf(array(
                'message' => 'Vui lòng nhập họ tên'
                    ))
        ));
        $this->add($name);
        //Username 
        $username = new Text('username');
        $username->setLabel('Usename');
        $username->addValidators(array(
            new PresenceOf(array(
                'message' => 'Vui lòng nhập username'
                    ))
        ));
        $this->add($username);
        // Email
        $email = new Text('email');
        $email->setLabel('E-Mail');
        $email->addValidators(array(
            new PresenceOf(array(
                'message' => 'Vui lòng nhập địa chỉ email'
                    )),
            new Email(array(
                'message' => 'Địa chỉ email không đúng'
                    ))
        ));
        $this->add($email);
        // Password
        $password = new Password('password');
        $password->setLabel('Password');
        $password->addValidators(array(
            new PresenceOf(array(
                'message' => 'Vui lòng nhập Password'
                    )),
            new Confirmation(array(
                'message' => 'Mật khẩu không khớp',
                'with' => 'confirmPassword'
                    ))            
        ));
        $this->add($password);
        // Confirm Password
        $confirmPassword = new Password('confirmPassword');
        $confirmPassword->setLabel('Confirm Password');
        $this->add($confirmPassword);
        //Địa chỉ
        $address = new Text('address');
        $address->setLabel('Address');
        $this->add($address);
        //Điểm ePoints
        $point = new Text('points');
        $point->setLabel('ePoints');
        $pointval = References::findFirstById('point');
        if ($pointval) {
            $point->setDefault($pointval->value);
        }
        $this->add($point);
        //Tài khoản
        $balance = new Text('balance');
        $balance->setLabel('Tài khoản');
        $balanceval = References::findFirstById('balance');
        $balance->setDefault($balanceval->value);
        $this->add($balance);
        //Phân quyền
        $role = new Select('role_id', Roles::find(), array(
            'using' => array('id', 'name')
        ));
        $role->setLabel('Quyền');
        $this->add($role);

        // CSRF
        $csrf = new Hidden('csrf');
        $csrf->addValidator(new Identical(array(
            'value' => $this->security->getSessionToken(),
            'message' => 'CSRF validation failed'
        )));
        $this->add($csrf);
        // Sign Up
        $this->add(new Submit('Submit', array(
            'class' => 'btn btn-success'
        )));
    }

    /**
     * Prints messages for a specific element
     */
    public function messages($name) {
        if ($this->hasMessagesFor($name)) {
            foreach ($this->getMessagesFor($name) as $message) {
                $this->flash->error($message);
            }
        }
    }
    public function appendMessage($message, $field, $type = null) {
        if (is_string($message)) {
            $message = new Message($message, $field, $type);
        }

        if ($message instanceof Message || $message instanceof ModelMessage) {
            // Check if there is a group for the field already.
            if (!is_null($this->_messages) && array_key_exists($field, $this->_messages)) {
                $this->_messages[$field]->appendMessage($message);
            } else {
                $this->_messages[$field] = new \Phalcon\Validation\Message\Group(array($message));
            }
        } else {
            throw new Exception("Can't append message into the form, invalid type.");
        }
    }


}
